/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.cicp.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author tangli
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-5-14 下午1:51:04          tangli         1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class CollectorTestService {

    /**
     * 
     */
    public CollectorTestService() {

    }

    /**
     * 利用反射创建的实例来调用其getParamMeta()方法，来获得对应采集类需要的参数。
     * @param collectorName
     * @return
     */
    public String[] getCollectorParamMeta(String collectorName) {
        Collector collector = getInstance(collectorName);
        String[] paramMeta = collector.getParamMeta();
        return paramMeta;
    }

    /**
     * 利用反射创建的实例来调用其getPropMeta()方法，来获得要采集的属性指标。
     * @param collectorName 采集类类名
     * @param params
     * @return
     */
    public String[] getCollectorPropMeta(String collectorName, Map<String, String> params) {
        Collector collector = getInstance(collectorName);
        collector.setParamMap(params);
        String[] propMeta = collector.getPropMeta();
        return propMeta;
    }

    /**
     * 利用反射创建的实例来调用其collect(ip)方法
     * @param collectorName 类名
     * @param params 
     * @param ip
     * @return
     * @throws CollectorException
     */
    public List<ResPropertyValue> collect(String collectorName, HashMap<String, String> params, String ip) throws CollectorException {
        Collector collector = getInstance(collectorName);
        collector.setParamMap(params);
        List<ResPropertyValue> list = collector.collect(ip);
        return list;
    }

    /**
     * 通过反射的方式创建采集类的一个实例
     * @param collectorName 采集类的类名
     * @return 采集类的实例（都继承了Collector类）
     */
    private Collector getInstance(String collectorName) {
        Collector collector = null;
        try {
            collector = (Collector) Class.forName(collectorName).newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return collector;
    }
}
